<template>
	<!-- 组织移位申请新增  -->
	<view class="">
		<dcustom bgColor="bg-gradual-purple" :isBack="true" :isArrow="true">
			<!-- <block slot="backText">返回</block> -->
			<block slot="content">组织移位申请</block>
		</dcustom>
		<view class="card-wrapper">
			<!-- 主表 -->
			<view class="radius shadow shadow-lg bg-white padding-sm light bg-blue">
				<view class="flex flex-wrap">
					<!-- <view class="basis-xs">
						
						单据编号
					</view>
					<view class="basis-xl text-black">
						{{ mainOrder.BILLNO }}
					</view> -->
				</view>

				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						集团<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black solid-bottom position-r" :class="activeLineIndex == 5 ? 'solid-bottom-blue' :'' "
					 @tap="activeIndexM(1,'GROUP')">
					
						<input class="wc-input " name="STORENAME" type="text" disabled :value="mainOrder.GROUPNAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a down-arrow-btn ">
							<text class="cuIcon-triangledownfill text-xxl text-bold" :class="activeLineIndex == 2 ? 'text-blue' :'' "></text>
						</view>
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						移出组织<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black solid-bottom position-r" :class="activeLineIndex == 5 ? 'solid-bottom-blue' :'' "
					 @tap="activeIndexM(11,'ORGNAME')">

						<input class="wc-input " name="ORGNAME" type="text" disabled :value="mainOrder.ORGNAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a down-arrow-btn ">
							<text class="cuIcon-triangledownfill text-xxl text-bold" :class="activeLineIndex == 2 ? 'text-blue' :'' "></text>
						</view>
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						移出仓库<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r solid-bottom" :class="isActiveStore? 'solid-bottom-blue' :'' ">
						<input class="wc-input" style="width: 60%;" name="STORENAME" type="text" :value="mainOrder.STORENAME" placeholder="请选择" />

						<view class="basis-xs text-right position-a scan-btn " @tap="openScan('STORENAME')">
							<text class="cuIcon-scan text-xxl text-bold"></text>
						</view>
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						申请人
					</view>
					<view class="basis-xl text-black">
						{{ mainOrder.MOVER || '' }}
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						移入组织<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black solid-bottom position-r" :class="activeLineIndex == 5 ? 'solid-bottom-blue' :'' "
					 @tap="activeIndexM(11,'INORGNAME')">

						<input class="wc-input " name="INORGNAME" type="text" disabled :value="mainOrder.INORGNAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a down-arrow-btn ">
							<text class="cuIcon-triangledownfill text-xxl text-bold" :class="activeLineIndex == 2 ? 'text-blue' :'' "></text>
						</view>
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						移入仓库<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r solid-bottom" :class="isActiveStore? 'solid-bottom-blue' :'' ">
						<input class="wc-input" style="width: 60%;" name="INSTORENAME" type="text" :value="mainOrder.INSTORENAME"
						 placeholder="请选择" />

						<view class="basis-xs text-right position-a scan-btn " @tap="openScan('INSTORENAME')">
							<text class="cuIcon-scan text-xxl text-bold"></text>
						</view>
					</view>
				</view>
				<view class="flex flex-wrap margin-top ">
					<view class="basis-xs uni-common-mt uni-common-pl">
						申请方式
					</view>
					<view class="uni-list">
						<radio-group @change="radioChange">
							<label class="uni-list-cell uni-list-cell-pd" v-for="(dataItme, index) in items" :key="dataItme.value"  @change="changeInput(index)">
								<view class="left">
									<radio :value="dataItme.value" :checked="index === current" />
								</view>
								<view class='right'>{{dataItme.name}}</view>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="flex flex-wrap margin-top ">
					<view class="basis-xs">
						备注:
					</view>
					<view class="basis-xl text-black solid-bottom" :class="isActiveNote? 'solid-bottom-blue' :'' ">
						<input @focus="activeNote" @blur="getNote" class="wc-input " type="text" value="" placeholder="请输入" />
					</view>
				</view>

				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						物资条码<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r" :class="isActiveStore? 'solid-bottom-blue' :'' ">

						<view class="basis-xs text-right position-a scan-btn " @tap="openScan('PK_MATERIAL')">
							<text class="cuIcon-scan text-xxl text-bold"></text>
						</view>
					</view>
				</view>
			</view>
			<!-- 子表 -->

			<view class="radius shadow shadow-lg bg-white padding-sm margin-top sub-table" v-for="(item,index) in dataList" :key="index">
				<view class="flex flex-wrap">
					<view class="basis-xs">
						物资编号
					</view>
					<view class="basis-xl text-black">
						{{ item.PK_MATERIAL }}
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						物资名称
					</view>
					<view class="basis-xl text-black">
						{{ item.MATERIALNAME }}
					</view>

				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						规格型号
					</view>
					<view class="basis-xl text-black">
						{{ item.MATSPEC }}
					</view>

				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						移出货位
					</view>
					<view class="basis-xl text-black">
						{{ item.STATIONNAME }}
					</view>

				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						移入货位<span class="text-red">*</span>
					</view>
					<view class="basis-xl text-black position-r solid-bottom" :class="isActiveStore? 'solid-bottom-blue' :'' ">
						<input class="wc-input" style="width: 60%;" name="INSTATIONNAME" type="text" :value="item.INSTATIONNAME"
						 placeholder="请选择" />

						<view class="basis-xs text-right position-a scan-btn " @tap="openScan('INSTATIONNAME',index)">
							<text class="cuIcon-scan text-xxl text-bold"></text>
						</view>
					</view>
					<!-- <view class="basis-xl text-black solid-bottom position-r" :class="activeLineIndex == 5 ? 'solid-bottom-blue' :'' "
					 @tap="activeIndexM(2,'BD_RACK')">

						<input class="wc-input " name="INSTATIONNAME" type="text" disabled :value="item.INSTATIONNAME" placeholder="请选择" />
						<view class="basis-xs text-right position-a down-arrow-btn ">
							<text class="cuIcon-triangledownfill text-xxl text-bold" :class="activeLineIndex == 2 ? 'text-blue' :'' "></text>
						</view>
					</view> -->
				</view>
				<view class="flex flex-wrap margin-top line-height-30 ">
					<view class="basis-xs ">
						数量<span class="text-red">*</span>
					</view>
					<view class="basis-xl solid-bottom  sub-table-input">
						<input class="wc-input" v-model="item.NNUM" type="text" placeholder="请输入移位数量" />
					</view>
				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						物资条码
					</view>
					<view class="basis-xl text-black">
						{{ item.BARCODE }}
					</view>

				</view>
				<view class="flex flex-wrap margin-top">
					<view class="basis-xs">
						计量单位
					</view>
					<view class="basis-xl text-black">
						{{ item.UNIT }}
					</view>

				</view>


			</view>


			<view class="">
				<button class="cu-btn block bg-blue margin-tb-sm lg" @click="submit">
					提交
				</button>
			</view>
		</view>

		<w-picker mode="dateTime" startYear="2000" endYear="2050" step="1" :defaultVal="[1,1,1,1,2,5]" :current="true"
		 @confirm="onConfirm" ref="dateTime" themeColor="#f00"></w-picker>

	</view>

</template>
<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import wPicker from "@/components/w-picker/w-picker.vue";
	import api from '@/common/api.js';
	import utils from '@/common/util.js';
	// var graceChecker = require("../../common/graceChecker.js");
	let query = {
		"category": "ST_INAPPLY",
		"page": 1,
		"rows": "50",
		"sidx": "",
		"sord": "",
		"queryJson": {},
		"userId": "",
		"account": "admin"
	}
	let _self, dateTimekey, activeIndex, VMEMO, BILLFROM, ajaxUrl, activeType;

	export default {
		computed: mapState(['userInfo', 'baseUrl']),
		data() {
			return {
				bstype: '',
				mainOrder: {},
				list: [], //获取到的物资数据
				dataList: [],
				allData: [], //初始化全局变量
				isActiveNote: false,
				isActiveStore: false,
				mid: '',
				today: utils.getToDay(),
				dateTimekey: '',
				activeIndex: '',
				label01: '单号', // 主表的第一个label
				activeLineIndex: 0,
				items: [{
						value: '0',
						name: '销售采购',
						checked: 'true'
					},
					{
						value: '1',
						name: '借用归还'
					},
				],
				current: 0,
				INSTATIONNAME:'',
				PK_INSTATION:'',
				VDEF1:0
			}
		},
		components: {
			wPicker
		},
		onLoad(p) {
			console.log(JSON.stringify(p));
			_self = this
			this.bstype = p.t
			query.userId = _self.userInfo.UserId
			query.account = _self.userInfo.Account
			query.category = p.dbt;
			
			/* console.log(p.item); */
			let abc = JSON.parse(decodeURIComponent(p.item));
			abc.PK_ORG = '';
			abc.ORGNAME = '';
			abc.STORENAME = '';
			abc.PK_STORE = '';
			abc.INSTORENAME = '';
			abc.PK_INSTORE = '';
			abc.INORGNAME='';
			abc.PK_INORG='';
			_self.mainOrder = abc;
			_self.mainOrder.PK_GROUP=abc.PK_GROUP;
			ajaxUrl = api.apiPublic.SaveDataMoveApply;

		},
		onShow() {
			uni.$on('getSelectItem', function(data) {
				console.log(data);
				switch (activeType) {
					case 'GROUP':
						_self.mainOrder.GROUPNAME = data.FullName
						_self.mainOrder.PK_GROUP = data.OrganizeId
						break;
					case 'BA_ORGANIZE':
						_self.mainOrder.ORGNAME = data.FullName
						_self.mainOrder.PK_ORG = data.OrganizeId
						break;
					case 'ORGNAME':
						_self.mainOrder.ORGNAME = data.text
						_self.mainOrder.PK_ORG = data.id
						break;	
					case 'INORGNAME':
						_self.mainOrder.INORGNAME = data.text
						_self.mainOrder.PK_INORG = data.id
						break;	
					case 'BD_RACK':
						console.log(data.VNAME)
						_self.dataList.INSTATIONNAME = data.VNAME;
						_self.dataList.PK_INSTATION = data.BILLNO;
						
					default:
						break;
				}
			})


		},
		onUnload() {
			uni.$off('getSelectItem', function(data) {})
		},
		methods: {			
			radioChange: function(evt) {
			            for (let i = 0; i < this.items.length; i++) {
			                if (this.items[i].value === evt.target.value) {
			                    this.current = i;
								_self.VDEF1=this.current;
								console.log(this.current);
			                    break;
			                }
			            }
			        },
			getNote(e) {
				VMEMO = e.target.value;
			},
			onConfirm(val) {
				this.dataList[activeIndex][dateTimekey] = val.result;
			},
			dateEvent(id, index) {
				dateTimekey = id;
				activeIndex = index;
				this.$refs.dateTime.show();
			},
			activeNote() {
				_self.isActiveNote = true
			},
			activeIndexM(i, t) {
				_self.activeLineIndex = i;
				console.log(i);
				if (i == 5) {
					return;
				}
				activeType = t;
				
				uni.navigateTo({
					url: '../public/search?t=' + t+"&organizeId="+_self.mainOrder.PK_GROUP+"&i="+i
				})
			},
			openScan: function(e, index) {
				//扫码
				uni.scanCode({
					onlyFromCamera: true, //只能从相机
					success: function(res) {
						let codeValue = res.result;
						let codeUrl = _self.baseUrl + api.apiPublic.ScanCode;
						console.log(e);
						console.log(codeValue);
						if (e == 'STORENAME') {
							let t = res.result.replace(/'/g, '"');
							let data = JSON.parse('{' + t + '}');
							_self.mainOrder.PK_STORE = data.PK_STORE;
							_self.mainOrder.STORENAME = data.STORENAME;
							/* _self.mainOrder.PK_ORG = data.PK_ORG;
							_self.mainOrder.ORGNAME = data.ORGNAME; */
						} else if (e == 'INSTORENAME') {
							let t = res.result.replace(/'/g, '"');
							let data = JSON.parse('{' + t + '}');
							_self.mainOrder.INSTORENAME = data.STORENAME;
							_self.mainOrder.PK_INSTORE = data.PK_STORE;
						} else if (e == 'PK_MATERIAL') {
							this.axios({
								url: codeUrl ,
								data: {
									Code: codeValue
								} ,
								isLoading: 1
							}).then(res=>{
									let { Result } = res ;
									let List = Result;
									if (_self.allData.length > 0) {
										let p = 0;
										_self.allData.forEach((item, index) => {
											if (item.BARCODE != List.BARCODE) {
												p += 0;

											} else {
												p += 1;
											}
										})
										if (p == 0) {
											_self.dataList.push(List);
										} else {
											uni.showToast({
												title: '不能重复扫该条码',
												icon: 'none',
												duration: 2000
											});
											return;
										}
									} else {
										console.log("iiiii");
										_self.dataList.push(List);
									}
									_self.allData = _self.dataList;
								});
						} else if (e == 'INSTATIONNAME') {
							let t = res.result.replace(/'/g, '"');
							let data = JSON.parse('{' + t + '}');
							_self.INSTATIONNAME= data.STATIONNAME;
							_self.PK_INSTATION= data.PK_INSTATION;
							_self.dataList[index].INSTATIONNAME= data.STATIONNAME||' iopi ';
							_self.dataList[index].PK_INSTATION= data.PK_STATION||' oii ';
						}
						console.log("获取二维码数据结束");
					}
				});
				console.log("相机加载结束");
			},

			submit() {
				let data = {
					"BTYPE": 2,
					"GROUPNAME": _self.mainOrder.GROUPNAME,
					"PK_GROUP": _self.mainOrder.PK_GROUP,
					"PK_STORE": _self.mainOrder.PK_STORE,
					"STORENAME": _self.mainOrder.STORENAME,
					"INSTORENAME": _self.mainOrder.INSTORENAME,
					"PK_INSTORE": _self.mainOrder.PK_INSTORE,
					"PK_ORG": _self.mainOrder.PK_ORG,
					"ORGNAME": _self.mainOrder.ORGNAME,
					"MOVER": _self.mainOrder.MOVER,
					"PK_MOVER": _self.mainOrder.PK_MOVER,
					"BILLDATE":  utils.getToDay(),
					"BILLMAKER": _self.mainOrder.BILLMAKER,
					"VDEF1":_self.VDEF1,
					"VMEMO": VMEMO,
					"userId": _self.userInfo.UserId,
					"account": _self.userInfo.Account,
					"Detail": JSON.stringify(_self.dataList)
				}
				console.log(data)
				let checkResult = false,
					error = '';
				_self.dataList.forEach((item) => {
					if (!item.INSTATIONNAME) {
						return error = '移入货位不能为空'
					} else if (!item.NNUM) {
						return error = '移入数量不能为空'
					}
					checkResult = true;
				})

				if (!data.PK_STORE) {
					return uni.showToast({
						title: '仓库不能为空',
						icon: "none"
					})
				} else if (data.PK_STORE == data.PK_INSTORE) {
					return uni.showToast({
						title: '移入仓库和移出仓库不能相同',
						icon: "none"
					})
				} else if (!checkResult) {
					return uni.showToast({
						title: error,
						icon: "none"
					})
				}
			
				this.axios({
					url: ajaxUrl ,
					data: data ,
					isLoading: 1
				}).then(res=>{
					let { Result } = res ;
					this.$utils.tips('提交成功')
					utils.delayBack();
				})
			}
		}
	}
</script>

<style scoped>
	.card-wrapper {
		padding: 26upx;
	}

	.line-height-30 {
		line-height: 24px !important;
	}



	.sub-table-input {
		margin-top: 10upx;
	}

	.down-arrow-btn {
		top: -20upx;
		right: 0upx;
		font-size: 40upx;
		padding: 10upx;
	}

	.add-btn-wrap {
		position: fixed;
		bottom: 100upx;
		right: 50upx;
	}

	.add-btn-wrap .cuIcon-add {
		font-size: 50upx;
	}
	label{
		width: 100%;
		height: 100upx;
	}
	.left{
		 float: left;
		 margin-right: 10upx;
	}
	.right{
		 float: left;
		 margin-right: 20upx;
	}
</style>
